using System;
using UnityEngine;

namespace Gj
{
    public class LoopTimer
    {
        private Action callback;

        private MonoBehaviour Dispatch;
        private int loop;
        private bool stop;
        private readonly float time;

        internal LoopTimer(MonoBehaviour dispatch, float t, Action CB)
        {
            Dispatch = dispatch;
            time = t;
            callback = CB;
            if (CB != null) Restart();
        }

        internal Func<bool> Need(int current)
        {
            return () => !stop && current == loop;
        }

        public void Restart(Action CB = null)
        {
            loop++;
            if (CB != null) callback = CB;
            Game.single.StartCoroutine(Game.WaitSecondsAsync(time, Need(loop), callback));
        }

        public void Restart(float first, Action CB = null)
        {
            loop++;
            if (CB != null) callback = CB;
            Game.single.StartCoroutine(Game.WaitSecondsAsync(first, Need(loop), callback));
        }

        public void Resume()
        {
            stop = false;
        }

        public void Stop()
        {
            stop = true;
        }

        public void Now()
        {
            if (callback != null) callback();
        }
    }

    public static class DateTools
    {
        public static DateTime GetZero()
        {
            return new DateTime(1970, 1, 1);
        }

        public static DateTime GetDate(int time)
        {
            return GetZero().AddSeconds(time);
        }

        public static DateTime GetDateByMilli(int time)
        {
            return GetZero().AddMilliseconds(time);
        }

        public static DateTime GetNow()
        {
            return DateTime.UtcNow;
        }

        public static int GetTime(DateTime date)
        {
            return (int)(date - GetZero()).TotalSeconds;
        }

        public static int GetNowTime()
        {
            return GetTime(GetNow());
        }

        public static DateTime Differ(DateTime d1, DateTime d2)
        {
            var _date = GetZero();
            if (d1 < d2) _date = _date.AddSeconds((d2 - d1).TotalSeconds);
            ;
            return _date;
        }

        // 根据秒返回文本
        public static string GetEasyFormatTime(int time)
        {
            var min = (time / 60).ToString();
            if (min.Length < 2) min = "0" + min;
            var sec = (time % 60).ToString();
            if (sec.Length < 2) sec = "0" + sec;
            return string.Format("{0}:{1}", min, sec);
        }

        public static bool IsToday(int time)
        {
            var date = GetDate(time);
            return date.ToString("yyyy-MM-dd") == GetNow().ToString("yyyy-MM-dd");
        }

        public static int FixLastTime(int lastTime, int time, int intervalTime, int frameTime)
        {
            if (time - lastTime > intervalTime + frameTime)
                return time;
            return lastTime + intervalTime;
        }

        public static long FixLastTime(long lastTime, long time, long intervalTime, long frameTime)
        {
            if (time - lastTime > intervalTime + frameTime)
                return time;
            return lastTime + intervalTime;
        }
    }
}